using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;

namespace SCG.SolarVengeance.Scenarios
{
    public class MirroredDoubleCluster : Scenario
    {
        //Create the Scenario
        public override void BuildScenario(int numPlayers, int capitalValues, int startingResources)
        {
            //Create Player 1's cluster
            int num = Random(10, 15);
            while (num > 0)
            {
                int x;
                int y;
                do
                {
                    x = Random(25, 40);
                    y = Random(60, 85);
                }
                while (x > 49);
                int value = Random(1, 20);
                if (value == 1)
                    value = Random(1, 20);
                int shields = Random(0, value - 1);
                int scanners = (int)(SVGame.RNG.Next(value) * 2.0d * SVGame.RNG.NextDouble());
                if (scanners == 1)
                    scanners = 0;
                double resources = SVGame.RNG.Next(value) * 3.0d * SVGame.RNG.NextDouble();
                StarSystem system = CreateStarSystem(x, y, value, shields, scanners, resources);

                //Assign a bonus
                if (Random(1,100) > 90)
                {
                    system.BonusValue = (int)(SVGame.RNG.Next(100) * SVGame.RNG.NextDouble());
                    if (system.BonusValue < 5 || system.BonusValue > 90)
                        system.BonusValue = 0;
                    system.BonusShipType = StarShip.ShipTypes[SVGame.RNG.Next(StarShip.ShipTypes.Count)];
                }
                num--;
            }

            //Mirror Player1's cluster to create Player2's cluster
            num = Game.StarSystems.Count;
            for (int i = 0; i < num; i++)
            {
                StarSystem system = Game.StarSystems[i];
                StarSystem mirror = CreateStarSystem(100 - system.X, 100 - system.Y, system.Value, system.Shields, system.Scanners, system.Resources);
                mirror.BonusShipType = system.BonusShipType;
                mirror.BonusValue = system.BonusValue;
            }

            //Create some nebula
            int numNebula = SVGame.RNG.Next(4) + 2;
            while (numNebula > 0)
            {
                CreateNebula(SVGame.RNG.Next(40) + 10, SVGame.RNG.Next(50) + 40, SVGame.RNG.Next(5) + 4, SVGame.RNG.Next(40) + 30);
                numNebula--;
            }

            //and mirror them
            for (int x = 0; x < 50; x++)
                for (int y = 40; y < 100; y++)
                {
                    TerrainType t = GetTerrain(x, y);
                    SetTerrain(100 - x, 100 - y, t);
                }

            //create some wormholes
            int numWormholes = SVGame.RNG.Next(4) + 1;
            while (numWormholes > 0)
            {
                CreateWormhole(SVGame.RNG.Next(40) + 9, SVGame.RNG.Next(40) + 49, SVGame.RNG.Next(40) + 50, SVGame.RNG.Next(40) + 10);
                numWormholes--;
            }

            //and mirror them
            numWormholes = Game.Wormholes.Count;
            for (int w = 0; w < numWormholes; w++)
            {
                Wormhole wh = Game.Wormholes[w];
                int x = 100 - wh.X;
                int destx = 100 - wh.DestX;
                int y = 100 - wh.Y;
                int desty = 100 - wh.DestY;
                CreateWormhole(x, y, destx, desty);
            }

            //Assign and mirror capitals
            numPlayers = Game.Players.Count;
            int idx = 0;
            while (numPlayers > 0)
            {
                //get a capital for player on left side
                StarSystem sys = null;
                do
                {
                    sys = Game.StarSystems[Random(0, Game.StarSystems.Count - 1)];
                }
                while (sys.Owner != null && sys.X >= 50);               
                Game.Players[idx].Capital = sys;

                //and mirror on right side
                idx++;
                numPlayers--;
                if (numPlayers > 0)
                {
                    SVMapObject svm = Game.GetObjectAt(100 - sys.X, 100 - sys.Y);
                    sys = svm as StarSystem;
                    Game.Players[idx].Capital = sys;
                    idx++;
                    numPlayers--;
                }
            }
        }

        //Scenario description
        public override string Description
        {
            get
            {
                return "Two perfectly mirrored Clusters are created in opposing corners of the map.  " +
                    "While this ensures a completely fair playing field, it also changes the dynamics of the " +
                    "game because you know exactly where your opponent is from the start!";
            }
        }

        //Author
        public override string Author
        {
            get
            {
                return "Beat2k";
            }
        }
    }
}
